package com.save_file_for_mysql;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName: MySQLSaveFileDemo
 * @Author: ZhangYue26
 * @Description: MySQL连接保存文件数据示例 注意PostgresSQL文件类型为 bytea ,MySQL为 longblob 类型
 * @Date: 2020-08-11
 */
public class MySQLSaveFileDemo {

    // 将图片插入数据库
    public static void insert() {
        String path = "D:/test.jpg";
        String filename = path.substring(path.lastIndexOf("/")+1, path.lastIndexOf("."));
        String suffix = path.substring(path.lastIndexOf(".")+1, path.length());
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;
        try {
            in = FileUtilForMySQL.readImage(path);
            conn = MySQLUtil.getConn();
            String sql = "insert into tb_file (id,name,suffix,file)values(?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 1);
            ps.setString(2, filename);
            ps.setString(3, suffix);
            ps.setBinaryStream(4, in, in.available());
            int count = ps.executeUpdate();
            if (count > 0) {
                System.out.println("插入成功！");
            } else {
                System.out.println("插入失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MySQLUtil.closeConn(conn);
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    // 读取数据库中图片
    public static void read() {
        String targetPath = "D:";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = MySQLUtil.getConn();
            String sql = "select * from tb_file where id =?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 1);
            rs = ps.executeQuery();
            while (rs.next()) {
                String fileName = rs.getString("name");
                String suffix = rs.getString("suffix");
                InputStream in = rs.getBinaryStream("file");
                FileUtilForMySQL.readBin2Image(in, targetPath+"\\"+fileName+"."+suffix);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MySQLUtil.closeConn(conn);
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    //测试
    public static void main(String[] args) {
//        insert();
        read();
    }
}